<?php

namespace App\Admin\Controllers;

use App\Models\Grade;
use App\Models\Member;
use App\Models\Program;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ProgramsController extends Controller
{
    use HasResourceActions;
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        $branch="";
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form($branch));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $branch=$this->getBranch();
        $grid = new Grid(new Program);
        $grid->model()->orderBy('id','desc');
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        if($branch=='admin') {
            $grid->id('Id');
            $grid->name('项目名');
            $grid->time('跨越时间');
            $grid->column( 'wd','武当评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===5){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $grid->column( 'gm','古墓评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===4){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $grid->column( 'xy','逍遥评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===3){
                        $test+=$value['usetime'];
                    }
                    if($value["program_id"]===$this->id && $value['branch_id']===6){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $grid->plan('当前进度');
            $states = [
                'on'  => ['value' => 0, 'text' => '开始', 'color' => 'success'],
                'off' => ['value' => 1, 'text' => '结束', 'color' => 'danger'],
            ];
            $grid->state('项目状态')->switch($states);
            $grid->ratiowd('武当绩效比例')->editable();
            $grid->ratiogm('古墓绩效比例')->editable();
            $grid->ratioxy('逍遥绩效比例')->editable();
            $grid->money('绩效奖金')->editable();
            $grid->column('项目绩效')->display(function () {
                return '<a href="http://192.168.0.200:8099/admin/member/grades?program_id='.$this->id.'"><button class="btn btn-info btn-sm">详细信息</button></a>';
            });
            $grid->updated_at('更新时间');

        }elseif ($branch=='wdmaster'){
            $grid->id('Id');
            $grid->name('项目名');
            $grid->time('跨越时间');
            $grid->plan('当前进度');
            $states = [
                'on'  => ['value' => 0, 'text' => '开始', 'color' => 'success'],
                'off' => ['value' => 1, 'text' => '结束', 'color' => 'danger'],
            ];
            $grid->state('项目状态')->switch($states);
            $grid->column( 'wd','武当评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===5){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $grid->column( 'gm','古墓评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===4){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $grid->column( 'xy','逍遥评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===3){
                        $test+=$value['usetime'];
                    }
                    if($value["program_id"]===$this->id && $value['branch_id']===6){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $grid->column('项目文件')->display(function () {
                return '<a href="http://192.168.0.200:8099/uploads/fujian/'.$this->fujian.'"><button class="btn btn-info btn-sm">下载附件</button></a>';
            });
            $grid->column('项目绩效')->display(function () {
                return '<a href="http://192.168.0.200:8099/admin/member/grades?program_id='.$this->id.'"><button class="btn btn-info btn-sm">详细信息</button></a>';
            });
            $grid->updated_at('更新时间');
        } else{
            switch ($branch){
                case 'manager':
                    $grid->id('Id');
                    $grid->name('项目名');
                    $grid->column('members', '参与成员')->display(function () {
                        $arr = [];
                        $member = Member::all()->toArray();
                        foreach ($this->members as $val) {
                            foreach ($member as $key => $item) {
                                if ($val == $item['id']) {
                                    array_push($arr, $member[$key]['name']);
                                }
                            }
                        }
                        return $arr;
                    });
                    $grid->usertime('完成天数');
                    $grid->plan('当前进度');
                    $states = [
                        'on'  => ['value' => 0, 'text' => '开始', 'color' => 'success'],
                        'off' => ['value' => 1, 'text' => '结束', 'color' => 'danger'],
                    ];
                    $grid->state('项目状态')->switch($states);
                    $grid->column('member.name','项目经理');
                    $grid->time('跨越时间');
                    $grid->column( 'wd','武当评估时间')->display(function (){
                        $time = Grade::all()->toArray();
                        $test=0;
                        foreach ($time as $value){
                            if($value["program_id"]===$this->id && $value['branch_id']===5){
                                $test+=$value['usetime'];
                            }
                        }
                        return $test;
                    });
                    $grid->column( 'gm','古墓评估时间')->display(function (){
                        $time = Grade::all()->toArray();
                        $test=0;
                        foreach ($time as $value){
                            if($value["program_id"]===$this->id && $value['branch_id']===4){
                                $test+=$value['usetime'];
                            }
                        }
                        return $test;
                    });
                    $grid->column( 'xy','逍遥评估时间')->display(function (){
                        $time = Grade::all()->toArray();
                        $test=0;
                        foreach ($time as $value){
                            if($value["program_id"]===$this->id && $value['branch_id']===3){
                                $test+=$value['usetime'];
                            }
                            if($value["program_id"]===$this->id && $value['branch_id']===6){
                                $test+=$value['usetime'];
                            }
                        }
                        return $test;
                    });
                    $grid->column('项目文件')->display(function () {
                        return '<a href="http://192.168.0.200:8099/uploads/fujian/'.$this->fujian.'"><button class="btn btn-info btn-sm">下载附件</button></a>';
                    });
                    $grid->updated_at('更新时间');
                    break;
                default:
                    $grid->id('Id');
                    $grid->name('项目名');
                    $grid->column('members', '参与成员')->display(function () {
                        $arr = [];
                        $member = Member::all()->toArray();
                        foreach ($this->members as $val) {
                            foreach ($member as $key => $item) {
                                if ($val == $item['id']) {
                                    array_push($arr, $member[$key]['name']);
                                }
                            }
                        }
                        return $arr;
                    });
                    $grid->usertime('完成天数');
                    $grid->plan('当前进度');
                    $grid->column('member.name','项目经理');
                    $grid->time('跨越时间');
                    $grid->column( 'wd','武当评估时间')->display(function (){
                        $time = Grade::all()->toArray();
                        $test=0;
                        foreach ($time as $value){
                            if($value["program_id"]===$this->id && $value['branch_id']===5){
                                $test+=$value['usetime'];
                            }
                        }
                        return $test;
                    });
                    $grid->column( 'gm','古墓评估时间')->display(function (){
                        $time = Grade::all()->toArray();
                        $test=0;
                        foreach ($time as $value){
                            if($value["program_id"]===$this->id && $value['branch_id']===4){
                                $test+=$value['usetime'];
                            }
                        }
                        return $test;
                    });
                    $grid->column( 'xy','逍遥评估时间')->display(function (){
                        $time = Grade::all()->toArray();
                        $test=0;
                        foreach ($time as $value){
                            if($value["program_id"]===$this->id && $value['branch_id']===3){
                                $test+=$value['usetime'];
                            }
                            if($value["program_id"]===$this->id && $value['branch_id']===6){
                                $test+=$value['usetime'];
                            }
                        }
                        return $test;
                    });
                    $grid->ratiowd('武当绩效比例');
                    $grid->ratiogm('古墓绩效比例');
                    $grid->ratioxy('逍遥绩效比例');
                    $grid->money('绩效奖金');
                    $grid->column('项目绩效')->display(function () {
                        return '<a href="http://192.168.0.200:8099/admin/member/grades?program_id='.$this->id.'"><button class="btn btn-info btn-sm">详细信息</button></a>';
                    });
                    $grid->updated_at('更新时间');
                    break;
            }

        }
        return $grid;
    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Program::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->members('Members');
        $show->usertime('Usertime');
        $show->plan('Plan');
        $show->time('Time');
        $show->timewd('Timewd');
        $show->timegm('Timegm');
        $show->timexy('Timexy');
        $show->ratiowd('Ratiowd');
        $show->ratiogm('Ratiogm');
        $show->ratioxy('Ratioxy');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $branch= $this->getBranch();
        $form = new Form(new Program);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableList();
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableReset();
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });
        if($branch=='admin'){
            $form->text('name', '项目名');
            $form->multipleSelect('members','参与成员')->options(Member::where('state',1)->where('branch_id','>',2)->where('branch_id','<',7)->pluck('name', 'id'));
            $form->text('usertime', '用时');
            $form->text('time', '时间跨度');
            $form->text('money', '绩效奖金');
            $form->text('ratiowd', '武当绩效占比');
            $form->text('ratiogm', '古墓绩效占比');
            $form->text('ratioxy', '逍遥绩效占比');
            $states = [
                'on'  => ['value' => 0, 'text' => '开始', 'color' => 'success'],
                'off' => ['value' => 1, 'text' => '结束', 'color' => 'danger'],
            ];

            $form->switch('state','项目状态')->states($states);
            $form->largefile('fujian','项目文件');
            $form->text('plan','当前进度')->disable();
            return $form;
        }else{
            $form->hidden('member_id', '项目经理')->default(Admin::user()->id);
            $form->text('name', '项目名');
            $form->multipleSelect('members')->options(Member::where('state',1)->where('branch_id','>',2)->where('branch_id','<',7)->pluck('name', 'id'));
            $form->text('usertime', '用时');
            $form->text('time', '时间跨度');
            $form->largefile('fujian','项目文件');
            $states = [
                'on'  => ['value' => 0, 'text' => '开始', 'color' => 'success'],
                'off' => ['value' => 1, 'text' => '结束', 'color' => 'danger'],
            ];

            $form->switch('state','项目状态')->states($states);
            }
            return $form;

    }
    public function getBranch(){
        $branch="";
      if(Admin::user()->isRole('administrator')){
            return $branch='admin';
        }elseif (Admin::user()->isRole('wdmaster')){
            return $branch='wdmaster';
        } elseif(Admin::user()->isRole('master')){
            if(strpos($_SERVER['REQUEST_URI'],'master')){
                $member= Member::all()->toArray();
                foreach ($member as $val){
                    if($val['username']==Admin::user()->username){
                        $branch=$val['branch_id'];
                    }
                }
                return $branch;
            }
        }
        elseif(Admin::user()->isRole('managers')){
            return $branch='manager';
        }
    }
}
